/*******************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Copyright (c) 2011 Michael Chan.
 ******************************************************************************/
package com.google.code.solrdimension.parsers.validators;

import java.util.List;

import com.google.code.solrdimension.parsers.beans.AlphaRangeDimensionValue;
import com.google.code.solrdimension.parsers.beans.Dimension;
import com.google.code.solrdimension.parsers.beans.Dimensions;
import com.google.code.solrdimension.parsers.beans.FloatRangeDimensionValue;
import com.google.code.solrdimension.parsers.beans.IntRangeDimensionValue;
import com.google.code.solrdimension.parsers.beans.SiftDimensionValue;
import com.google.code.solrdimension.parsers.beans.Solrdimension;

/**
 * 
 * Simple validations performed on top of the XSD
 * 
 * @author Michael Chan
 * 
 */
public class ParserBeansValidator {

    public static boolean ValidateDimension(Solrdimension solrdimension) {

        boolean valid = true;

        if (solrdimension != null) {

            Dimensions dimensionsElement = solrdimension.getDimensions();

            List<Dimension> dimensions = dimensionsElement.getDimension();

            if (dimensions != null && !dimensions.isEmpty()) {

                for (Dimension dimension : dimensions) {

                    valid = validateAlphaRangeDimensionValues(dimension.getAlphaRangeDimensionValue());
                    if (!valid) {
                        break;
                    }
                    valid = validateSiftDimensionValues(dimension.getSiftDimensionValue());
                    if (!valid) {
                        break;
                    }
                    valid = validateIntRangeDimensionValues(dimension.getIntRangeDimensionValue());
                    if (!valid) {
                        break;
                    }
                    valid = validateFloatRangeDimensionValues(dimension.getFloatRangeDimensionValue());
                    if (!valid) {
                        break;
                    }

                }

            } else {
                valid = false;
            }

        } else {
            valid = false;
        }

        return valid;
    }

    private static boolean validateAlphaRangeDimensionValues(List<AlphaRangeDimensionValue> alphaRangeDimVals) {

        boolean result = true;

        if (alphaRangeDimVals != null && !alphaRangeDimVals.isEmpty()) {

            for (AlphaRangeDimensionValue alphaRangeDimensionValue : alphaRangeDimVals) {

                Character lower = alphaRangeDimensionValue.getLowerbound().charAt(0);

                Character upper = alphaRangeDimensionValue.getUpperbound().charAt(0);

                // Comparing two char
                if (lower.compareTo(upper) >= 0) {
                    result = false;
                    break;
                }
            }

        }
        // getting empty set is valid
        return result;

    }

    private static boolean validateSiftDimensionValues(List<SiftDimensionValue> siftDimVals) {

        boolean result = true;

        if (siftDimVals != null && !siftDimVals.isEmpty()) {

            for (SiftDimensionValue siftRangeDimensionValue : siftDimVals) {

                Character lower = siftRangeDimensionValue.getLowerbound().charAt(0);

                Character upper = siftRangeDimensionValue.getUpperbound().charAt(0);

                // Comparing two char
                if (lower.compareTo(upper) >= 0) {
                    result = false;
                    break;
                }
            }

        }

        // getting empty set is valid
        return result;

    }

    private static boolean validateIntRangeDimensionValues(List<IntRangeDimensionValue> intDimVals) {

        boolean result = true;

        if (intDimVals != null && !intDimVals.isEmpty()) {

            for (IntRangeDimensionValue intRangeDimensionValue : intDimVals) {

                int lower = intRangeDimensionValue.getLowerbound();

                int upper = intRangeDimensionValue.getUpperbound();

                // Comparing two char
                if (upper <= lower) {
                    result = false;
                    break;
                }
            }

        }

        // getting empty set is valid
        return result;

    }

    private static boolean validateFloatRangeDimensionValues(List<FloatRangeDimensionValue> floatDimVals) {

        boolean result = true;

        if (floatDimVals != null && !floatDimVals.isEmpty()) {

            for (FloatRangeDimensionValue floatRangeDimensionValue : floatDimVals) {

                float lower = floatRangeDimensionValue.getLowerbound();

                float upper = floatRangeDimensionValue.getUpperbound();

                // Comparing two char
                if (upper <= lower) {
                    result = false;
                    break;
                }
            }

        }

        // getting empty set is valid
        return result;

    }

}
